﻿using System;
using System.Collections.Generic;

namespace Titan.FIS.LMS.Helper
{
    public static class CountValueHelp
    {
        public static decimal ValueMeliorate46(decimal value, int count)
        {
            return (decimal)Math.Round((decimal)value, count);
        }


        public static bool CountValue(int count, decimal cdt1, decimal cdt2, decimal va1, decimal va2, decimal va3, decimal[] value, ref List<string> SelectValueList, ref decimal EffectiveValue)
        {
            var result = false;
            if (count == 2)
            {
                result = CountValueTwo(cdt1, cdt2, va1, va2, va3, value, ref SelectValueList, ref EffectiveValue);
            }
            else if (count == 3)
            {
                result = CountValueThree(cdt1, cdt2, va1, va2, va3, value, ref SelectValueList, ref EffectiveValue, false);
            }
            else if (count >= 4)
            {
                result = CountValueFour(cdt1, cdt2, va1, va2, va3, value, ref SelectValueList, ref EffectiveValue);
            }
            return result;
        }

        public static bool CountValueTwo(decimal cdt1, decimal cdt2, decimal va1, decimal va2, decimal va3, decimal[] value, ref List<string> SelectValueList, ref decimal EffectiveValue)
        {
            bool result = false;
            for (int j = 0; j < value.Length - 1; j++)
            {
                if (value[j + 1] > 0 && value[j + 1] < cdt1)
                {
                    if (value[j] - value[j + 1] <= va1)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1]) / 2;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 1] >= cdt1 && value[j + 1] <= cdt2 && value[j + 1] != 0)
                {
                    if (value[j] - value[j + 1] <= va2)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1]) / 2;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 1] > cdt2)
                {
                    if (value[j] - value[j + 1] <= va3)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1]) / 2;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }

        public static bool CountValueThree(decimal cdt1, decimal cdt2, decimal va1, decimal va2, decimal va3, decimal[] value, ref List<string> SelectValueList, ref decimal EffectiveValue, bool bFour)
        {
            bool result = false;
            for (int j = 0; j < value.Length - 2; j++)
            {
                if (value[j + 2] > 0 && value[j + 2] < cdt1)
                {
                    if (value[j] - value[j + 2] <= va1 * (decimal)1.2)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2]) / 3;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 2] >= cdt1 && value[j + 2] <= cdt2 && value[j + 2] != 0)
                {
                    if (value[j] - value[j + 2] <= va2 * (decimal)1.2)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2]) / 3;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 2] > cdt2)
                {
                    if (value[j] - value[j + 2] <= va3 * (decimal)1.2)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2]) / 3;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        result = true;
                        break;
                    }
                }
            }
            //if (!result && !bFour)
            //{
            //    result = CountValueTwo(cdt1, cdt2, va1, va2, va3, value, ref SelectValueList, ref EffectiveValue);
            //}
            return result;
        }

        public static bool CountValueFour(decimal cdt1, decimal cdt2, decimal va1, decimal va2, decimal va3, decimal[] value, ref List<string> SelectValueList, ref decimal EffectiveValue)
        {
            bool result = false;
            for (int j = 0; j < value.Length - 3; j++)
            {
                if (value[j + 3] > 0 && value[j + 3] < cdt1)
                {
                    if (value[j] - value[j + 3] <= va1 * (decimal)1.3)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2] + value[j + 3]) / 4;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        SelectValueList.Add(value[j + 3].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 3] >= cdt1 && value[j + 3] <= cdt2 && value[j + 3] != 0)
                {
                    if (value[j] - value[j + 3] <= va2 * (decimal)1.3)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2] + value[j + 3]) / 4;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        SelectValueList.Add(value[j + 3].ToString());
                        result = true;
                        break;
                    }
                }
                else if (value[j + 3] > cdt2)
                {
                    if (value[j] - value[j + 3] <= va3 * (decimal)1.3)
                    {
                        EffectiveValue = (value[j + 0] + value[j + 1] + value[j + 2] + value[j + 3]) / 4;
                        SelectValueList.Add(value[j + 0].ToString());
                        SelectValueList.Add(value[j + 1].ToString());
                        SelectValueList.Add(value[j + 2].ToString());
                        SelectValueList.Add(value[j + 3].ToString());
                        result = true;
                        break;
                    }
                }
            }
            if (!result)
            {
                result = CountValueThree(cdt1, cdt2, va1, va2, va3, value, ref SelectValueList, ref EffectiveValue, true);
            }
            return result;
        }
    }
}
